package com.learn.acm.one;


import org.junit.jupiter.api.Test;

/**
 * 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的。
 *
 * 示例 1:
 *
 * 输入: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
 * 输出: true
 * 示例 2:
 *
 * 输入: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
 * 输出: false
 *"aa"
 * "ab"
 * "aaba"
 */
public class Solution97 {


    @Test
    public void testIsInterleave() {
        String s1 = "aabcc";
        String s2 = "dbbca";
        String s3 = "aadbbcbcac";
        System.out.println(isInterleave(s1,s2,s3));
    }

    public boolean isInterleave(String s1, String s2, String s3) {
        if (s1.length() + s2.length() != s3.length()) {
            return false;
        }
        int s1Point = 0,s2Point = 0,s3Point = 0;
        while (s1Point<s1.length() || s2Point < s2.length() || s3Point < s3.length()) {
            while (s1Point<s1.length() && s1.charAt(s1Point) == s3.charAt(s3Point)) {
                s1Point++;
                s3Point++;
            }
            while (s2Point<s2.length() && s2.charAt(s2Point) == s3.charAt(s3Point)) {
                s2Point++;
                s3Point++;
            }
            if (s1Point==s1.length() && s2Point==s2.length() && s3Point==s3.length()) return true;
            if ((s1Point>=s1.length() || s1.charAt(s1Point) != s3.charAt(s3Point)) &&
                    (s2Point >= s2.length() || s2.charAt(s2Point) != s3.charAt(s3Point))) {
                return false;
            }
        }

        return s3Point==s3.length() ? true:false;
    }
}
